package com.emep.emeptime.service;

import com.emep.emeptime.entity.SysAqiInfo;
import com.emep.emeptime.entity.SysAreaInfo;
import com.emep.emeptime.entity.Weather;
import com.emep.emeptime.model.ApiData;
import com.emep.emeptime.model.Tianqi;
import com.emep.emeptime.repository.SysAqiInfoRepository;
import com.emep.emeptime.repository.SysAreaInfoRepository;
import com.emep.emeptime.repository.WeatherRepository;
import com.emep.emeptime.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author wangcc
 * @Description: 天气预报业务层
 * @date 2018/3/279:03
 * @version V1.0
 */
@Service
@Slf4j
public class WeatherService {

    @Resource
    private WeatherRepository weatherRepository;
    @Resource
    private SysAreaInfoService sysAreaInfoService;
    /**
     *
     * 新增排名数据
     *
     * @param payload
     * @return
     * @throws Exception
     */
    public Weather save(Tianqi payload) {
        Weather weather = new Weather();
        String CityCode = getAreaId(payload.getResult().getToday().getCity());
        try {
            //地区
            weather.setCityCode(CityCode);
            //温度
            weather.setTemp(payload.getResult().getSk().getTemp());
            //风向
            weather.setWindDirection(payload.getResult().getSk().getWind_direction());
            //风级
            weather.setWindStrength(payload.getResult().getSk().getWind_strength());
            //时间
            weather.setTime(payload.getResult().getSk().getTime());
            //当天温度
            weather.setTemperature(payload.getResult().getToday().getTemperature());
            //当天天气
            weather.setWeather(payload.getResult().getToday().getWeather());
            //当天风向及风级
            weather.setWind(payload.getResult().getToday().getWind());
            //当天星期几
            weather.setWeek(payload.getResult().getToday().getWeek());
            //当天时间yyyy-mm-dd
            weather.setDatey(payload.getResult().getToday().getDate_y());
            //保存时间
            weather.setSaveDate(String.valueOf(System.currentTimeMillis()));
            weather = weatherRepository.save(weather);
            if(weather !=null && !StringUtils.isEmpty(weather.getId())){
                log.info("*************** 天气预报数据插入成功 **************");
            }else {
                log.error("*************** 天气预报数据插入失败 **************");
            }
        }catch (Exception e){
            log.error(e.getClass().getName()+":"+ e.getMessage());
            log.error(e.getLocalizedMessage());
        }
        return weather;
    }
    /**
     * 获取行政区划code
     * @param name
     * @return
     */
    public String getAreaId(String name){
        String areaCode = "";
        try {
            SysAreaInfo sysAreaInfo = sysAreaInfoService.findAreaByAreaName("%" + name + "%");
            if(sysAreaInfo != null ) {

                areaCode = sysAreaInfo.getAreaCode();
            }
        }catch (Exception e){
            log.error(e.getLocalizedMessage());
        }
        return areaCode;
    }
}
